/**
 * Created by Administrator on 2017/10/4 0004.
 */
(function ($j) {
    function init() {
        renderData();
        bindBtnEvent();
    };

    function renderData() {
        $.showPreloader();
        //获得用户信息
        $j.restPost('/user/doGetUserInfo').done(function (mess, data) {
            if (data.phone == undefined) {
                $('#perfectFirst').show();
                $('#phoneCode').hide();
            } else {
                $('#perfectFirst').hide();
                $('#phoneCode').show();
                $('#phone').html(data.phone);
            }
            //获得用户预购产品信息
            $j.restPost('/order/doQueryOrderInfo',{params:'安装'}).done(function (mess, data) {
                if (data.list.length > 0) {
                    var content = '';
                    $('#leasing').hide();
                    $('#leasing_card').show();
                    $(data.list).each(function (i, item) {
                        content += '<li style="overflow: hidden">' +
                            '<a href="javascript:void(0);" class="item-link item-content" data-id="' + item.orderId + '" data-deviceid = "' + item.deviceId + '" style="width:100%;position: relative;left: 0;transition:all 0.2s;">' +
                            '<div class="item-media"><img src="/res/imgs/products/prod/' + item.deviceId + '.jpg" style="width: 4rem;"></div>' +
                            '<div class="item-inner">' +
                            '<div class="item-title-row">' +
                            '<div class="item-title">' + item.deviceName + '</div>' +
                            '</div>' +
                            '<div class="item-text">' + item.deviceInfo + '</div>' +
                            '<div class="item-subtitle">租贷时间：' + $j.dateFormatUtil(item.createTime) + '</div>' +
                            '</div>' +
                            '</a><div class="li-btn-delete" data-id="' + item.orderId + '" style="transition:all 0.2s;">删除</div>' +
                            '</li>';
                    });
                    $('#leasing_card').html(content);
                    // var list = document.getElementById("leasing_card");
                    // new SwipeOut(list);
                    $('#leasing_card li').swipeLeft(function () {
                        $(this).addClass("left-active");
                    });

                    $('#leasing_card li').swipeRight(function () {
                        $(this).removeClass("left-active");
                    });

                    $('#leasing_card li .li-btn-delete').on('click', function (event) {
                        var $this = $(this);
                        var orderId = $(this).data('id');
                        $.confirm('确定删除这条预定记录吗', function () {
                            $.showPreloader('正在删除');
                            $j.restPost('/order/doRemove/' + orderId).done(function(mess,data){
                                init();
                                $.hidePreloader();
                            }).fail(function(mess){
                                $.toast(mess);
                                $.hidePreloader();
                            })
                        });
                        event.stopPropagation();
                    });

                } else {
                    $('#leasing').show();
                    $('#leasing_card').hide();
                }
                $.hidePreloader();
            }).fail(function (mess) {
                $.toast(mess);
            });
        }).fail(function (mess) {
            $.toast(mess);
            $.hidePreloader();
        });

    };

    function bindBtnEvent() {

        $('.integral,.sign').off('click').on('click', function () {
            $.toast('暂未开放');
        });

        $('.p_info').off('click').on('click', function () {
            window.location.href = "/user/user_info";
        });

        $('.recommond').off('click').on('click', function () {
            window.location.href = "/user/recommend";
        });

        $(document).off('click', '#leasing_card li a').on('click', '#leasing_card li a', function () {
            window.location.href = "/user/rent?orderId=" + $(this).data('id') + '&deviceId=' + $(this).data('deviceid');
        });

        $(document).on('click', function () {
            $('#leasing_card li').removeClass("left-active");
        });

        $('#perfectFirst').off('click').on('click', function () {
            window.location.href = "/user/user_info";
        });

        $('.toProdPage').off('click').on('click', function () {
            window.location.href = "/user/adv";
        });

        $('.system').off('click').on('click', function () {
            $j.restPost('/user/doGetSessionUser').done(function (mess, data) {
                if (data.authen == "1") {
                    window.location.href = '/user/manage';
                } else {
                    $.prompt('请输入管理员密码', function (val) {
                        $j.restPost('/user/doCheckPassword', {password: val}).done(function (mess, data) {
                            window.location.href = '/user/manage';
                        }).fail(function (mess) {
                            $.toast(mess);
                        })
                    });
                }
            }).fail(function (mess) {
                $.toast(mess);
            });
        });
    }

    init();
})(jQuery);